package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.UserInfo;
import com.example.mapper.UserInfoMapper;
import com.example.service.HeadTypeSrvice;
import com.example.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@RestController
@RequestMapping("/userInfo")
public class UserInfoController implements WebMvcConfigurer {

    @Resource
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private HeadTypeSrvice headTypeSrvice;


    @GetMapping("get{id}")
    public UserInfo get(Long id) {
        //创建两个图片集合
        List listOne = new ArrayList<>();
        List listTwo = new ArrayList<>();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        UserInfo userInfo = userInfoMapper.selectById(id);
//        查询所有轮播图
        String path = "/images/ims/" + userInfo.getNeiron() + "/轮播图/";
        File file = new File(path);
        File[] ts = file.listFiles();
        for (File tse : ts) {
            listOne.add(tse.toString().substring(path.length()));
        }
        userInfo.setSlideshow(listOne);

        String paths = "/images/ims/" + userInfo.getNeiron() + "/详情介绍/";
        File files = new File(paths);
        File[] tes = files.listFiles();
        for (File tse : tes) {
            listTwo.add(tse.toString().substring(paths.length()));
        }
        userInfo.setDetails(listTwo);

        return userInfo;
    }

    /**
     * @return
     */
    @GetMapping("/findAll")
    public Map<String, Object> findAll() {
        List<UserInfo> bigTypeList = userInfoService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("message", bigTypeList);
        map.put("messages", "1234");
        return map;
    }

    @PostMapping("/add")
    public Boolean add(@RequestBody UserInfo userInfo) {
        userInfo.setCreateTime(LocalDateTime.now());
        return userInfoService.save(userInfo);
    }


//    @GetMapping("/list")
//    public List<HeadType> list() {
//        List<HeadType> list = headTypeSrvice.list();
//        for (HeadType headType : list){
//            List<UserInfo> name = userInfoService.list(new QueryWrapper<UserInfo>().eq("head_type", headType.getName()));
//            headType.setSubType(name);
//        }
//
//        return list;
//    }

    /**
     * 用户下单 数据库数量增加
     *
     * @param id
     * @param shuliang
     * @return boolean
     */
    @GetMapping("/sumupdate{id}{shuliang}")
    public boolean update2(Long id, int shuliang) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        UserInfo userInfo = new UserInfo();
        userInfo.setShuliang(shuliang);
        updateWrapper.eq("id", id);
        userInfoService.update(userInfo, updateWrapper);
        return userInfoService.update(userInfo, updateWrapper);

    }

    /**
     * 分页查询
     *
     * @param a
     * @param b
     * @return
     */
    @GetMapping("/page")
    public List<UserInfo> page(int a, int b) {
        Page<UserInfo> employees = userInfoMapper.selectPage(new Page<>(a, b), null);
        List list = new ArrayList<>();
        list.add(employees.getRecords());
        list.add(employees.getTotal());
        return list;
    }

    /**
     * 条件查询 id
     */

    @RequestMapping("/like")
    public List<UserInfo> selectList(UserInfo userInfo) {
//        String path = "/images/ims/" + userInfo.getNeiron();
//        String[] strings =new String[]{"轮播图","详情图片"};
//        List<UserInfo> list = userInfoService.selectList(userInfo);
//        File file = new File(path);
//        List list1 = new ArrayList<>();
//        List list2 = new ArrayList<>();
//        for (String Strin : strings){
//            String[] File_list =new File ("/images/ims/" + userInfo.getNeiron() + Strin ).list();
//            if (Strin.equals("轮播图")){
//                for (UserInfo userInfo1 : list){
//                    for ( String File_lists : File_list){
//                        list1.add(File_lists);
//                        System.out.println(File_lists);
//                    }
//                    userInfo1.setSlideshow(list1);
//                }
//            }else {
//                for (UserInfo userInfo1 : list){
//                    for ( String File_lists : File_list){
//                        list2.add(File_lists);
//                        System.out.println(File_lists);
//                    }
//                    userInfo1.setDetails(list2);
//                }
//            }
//
//        }

        if (null != userInfo.getNeiron()) {
            return userInfoMapper.selectList(new QueryWrapper<UserInfo>().like("neiron", userInfo.getNeiron()));
        } else {
            return userInfoMapper.selectList(new QueryWrapper<UserInfo>().like("head_type", userInfo.getHeadType()));
        }


    }
    /**
     * 设置轮播图和 详情图片
     *
     */

}
